package com.yang.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mzt.logapi.starter.annotation.LogRecord;
import com.yang.dao.entity.AdminDO;
import com.yang.dao.mapper.AdminMapper;
import com.yang.dto.req.AdminLoginReqDTO;
import com.yang.dto.resp.AdminLoginRespDTO;
import com.yang.service.AdminService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


/**
 * 管理员业务实现层
 * 作者：孤城暖阳y
 * 开发时间：2025-02-26
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AdminServiceImpl extends ServiceImpl<AdminMapper, AdminDO> implements AdminService {

    private final AdminMapper adminMapper;

    @LogRecord(
            success = """
                    账号：{{#requestParam.username}}， \
                    密码：{{#requestParam.password}}， \
                    最后登录ip：{{#requestParam.lastLoginIp}}， \
                    """,
            type = "OrderQuery",
            bizNo = "{{#requestParam.username}}",
            extra = "{{#requestParam.toString()}}"
    )
    @Override
    public AdminLoginRespDTO login(AdminLoginReqDTO requestParam) {
        System.out.println(requestParam.getUsername());
        // 查询数据库
        LambdaQueryWrapper<AdminDO> queryWrapper = Wrappers.lambdaQuery(AdminDO.class)
                .eq(AdminDO::getUsername, requestParam.getUsername());
        AdminDO adminDO = adminMapper.selectOne(queryWrapper);
        // 判断是否查询到数据
        if (adminDO != null) {
            // 更新最后登录ip
            adminDO.setLastLoginIp(requestParam.getLastLoginIp());
            // 更新最后登录时间
            adminMapper.updateById(adminDO);
            // 登录成功，返回true
            return BeanUtil.toBean(adminDO, AdminLoginRespDTO.class);
        }
        return null;
    }
}
